/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.jsimplemarket.wicket.pages;
    
import com.googlecode.jsimplemarket.model.RaportIncasari;
import com.googlecode.jsimplemarket.model.TotalIncasari;
import com.googlecode.jsimplemarket.service.InchiriereService;

import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;
    
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
    
/**
 *
 * @author Alexandru.Codescu
 */
@SuppressWarnings("serial")
public class RaportIncasariZilnicePage extends DefaultPageTemplate {
    @SpringBean(name = "inchiriereService")
    private InchiriereService inchiriereService;
    private Date dataRaport = new Date();
    private Locale locale = new Locale("ro","RO");
    private NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
    
    public RaportIncasariZilnicePage() {
        final Form<RaportIncasariZilnicePage> form = new Form<RaportIncasariZilnicePage>("form");
        form.setModel(new CompoundPropertyModel<RaportIncasariZilnicePage>(this));
        
        final DateTextField tfDataRaport = new DateTextField("dataRaport", new PropertyModel<Date>(this, "dataRaport"), "dd.MM.yyyy");
        tfDataRaport.add(new DatePicker());        
        form.add(tfDataRaport);
        
        final RaportIncasariView raportView = new RaportIncasariView("raport", inchiriereService.queryIncasariByData(new Date()));
        final TotalIncasariView totalView = new TotalIncasariView("raportTotal", inchiriereService.queryTotalIncasariByData(new Date()));
 
        final WebMarkupContainer wkRaport = new WebMarkupContainer("wkRaport");
        wkRaport.setOutputMarkupId(true);
        wkRaport.setOutputMarkupPlaceholderTag(true);
        wkRaport.add(raportView,totalView);
        
        form.add(wkRaport);
        form.add(new AjaxSubmitLink("submit") {

            @Override
            public void onSubmit(AjaxRequestTarget target, Form form) {
                super.onSubmit(target, form);
                tfDataRaport.processInput();
                List<RaportIncasari> incasari = inchiriereService.queryIncasariByData(tfDataRaport.getConvertedInput());
                raportView.setList(incasari);
                raportView.modelChanged();
                
                List<TotalIncasari> total = inchiriereService.queryTotalIncasariByData(tfDataRaport.getConvertedInput());
                totalView.setList(total);
                totalView.modelChanged();
              
                target.add(wkRaport);
            }
            
        });
        add(form);
    }
    
    class RaportIncasariView extends ListView<RaportIncasari> {
        
        public RaportIncasariView(String id, List<RaportIncasari> list) {
            super(id, list);
        }

        @Override
        protected void populateItem(ListItem<RaportIncasari> li) {
            int index = li.getIndex() + 1;
            RaportIncasari raport = li.getModelObject();
                    
            Label lblIndex = new Label("index", index);
            Label lblDenumireTaxa = new Label("denumireTaxa", raport.getDenumireTaxa());
            Label lblNrSpatii = new Label("nrSpatii", raport.getNrSpatii());
            Label lblValoareTaxa = new Label("valoareTaxa", raport.getValoareTaxa());
            Label lblValoareTotala = new Label("valoareTotala", raport.getValoareTotala()==null ? "0" : fmt.format(raport.getValoareTotala()));
            
            li.add(lblIndex, lblDenumireTaxa, lblNrSpatii, lblValoareTaxa, lblValoareTotala);
        }
    }
    
    class TotalIncasariView extends ListView<TotalIncasari> {
        
        public TotalIncasariView(String id, List<TotalIncasari> list) {
            super(id, list);
        }

        @Override
        protected void populateItem(ListItem<TotalIncasari> li) {
            int index = li.getIndex() + 1;
            TotalIncasari raport = li.getModelObject();
            Label lblValoareTotala = new Label("valTotala", raport.getValoareTotala()==null ? "0" : fmt.format(raport.getValoareTotala()));
            Label lblToatalTarabe = new Label("totalTarabe", raport.getTotalSpatii());
            li.add(lblValoareTotala,lblToatalTarabe);
        }
    }
}
